{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31693511",
   "metadata": {},
   "source": [
    "# 第3周\n",
    "* author:邱星倩\n",
    "* time：第3周周二上午\n",
    "* link: [face++](https://console.faceplusplus.com.cn/documents/4887586)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01435290",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "633548da",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "API_Key = \"6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft\"\n",
    "API_secret = \"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27e3754e",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca1f8a3a",
   "metadata": {},
   "source": [
    "# 1.人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "58861ec1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 131,\n",
       " 'request_id': '1647702152,09d7eda6-ef62-434e-a6ad-f722e07d6d61',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 43},\n",
       "    'smile': {'threshold': 50.0, 'value': 88.936}},\n",
       "   'face_rectangle': {'width': 48, 'top': 43, 'left': 56, 'height': 48},\n",
       "   'face_token': '34699e256bf5be7e74a3fd2a48b150a6'}]}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "payload = {\n",
    "    \"api_key\":'6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft',\n",
    "    \"api_secret\":\"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\",\n",
    "    \"face_tokens\":\"34699e256bf5be7e74a3fd2a48b150a6\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7c5c27f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4d48bd1",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05d96ae9",
   "metadata": {},
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7277ace2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647702269,70c03176-2dfb-40af-b799-8b4884ffde60',\n",
       " 'time_used': 743,\n",
       " 'faces': [{'face_token': '1be78a71471c6fba325f2f68e46b7d5f',\n",
       "   'face_rectangle': {'top': 81, 'left': 84, 'width': 42, 'height': 42},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 30},\n",
       "    'smile': {'value': 93.981, 'threshold': 50.0}}}],\n",
       " 'image_id': '/v+0lAskTQH0hYXQObyd/A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸检测 ——> 找出face-token\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "payload = {\n",
    "    \"api_key\":'6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft',\n",
    "    \"api_secret\":\"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\",\n",
    "    \"image_url\":\"https://tse1-mm.cn.bing.net/th/id/OIP-C.NUF2svgSGOHXnk9HBNcOfwHaJ6?w=185&h=248&c=7&r=0&o=5&pid=1.7\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ed48d3d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647702351,d1336b7c-ba84-4689-b8c0-f15130c7af51',\n",
       " 'time_used': 301,\n",
       " 'confidence': 38.408,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先人脸检测再人脸比对\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":'6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft',\n",
    "    \"api_secret\":\"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\",\n",
    "    \"face_token1\":\"34699e256bf5be7e74a3fd2a48b150a6\",\n",
    "    \"face_token2\":\"1be78a71471c6fba325f2f68e46b7d5f\"\n",
    "}\n",
    "# 当 confident > le-5 为同一个人\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "fb6f8a11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7ed7d79",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d21cc92f",
   "metadata": {},
   "source": [
    "# 3.封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80533af1",
   "metadata": {},
   "source": [
    "## （1）人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "20d96be5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_Key,API_Secret,image_url,landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"image_url\":image_url,\n",
    "        \"return_landmark\":landmark,\n",
    "        \"return_attributes\":attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "288a4011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647702361,a846909b-4ff2-4317-92f5-1a37bc452b78',\n",
       " 'time_used': 519,\n",
       " 'faces': [{'face_token': 'fb5cf63816db5fdefe95097533c22703',\n",
       "   'face_rectangle': {'top': 53, 'left': 69, 'width': 48, 'height': 48}}],\n",
       " 'image_id': 'p/1GwnuGYECOM6WHPEvFsQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(API_Key,API_secret,'https://tse1-mm.cn.bing.net/th/id/OIP-C.zRZvY2s8xyyYqP4_NS14awHaLH?w=185&h=278&c=7&r=0&o=5&pid=1.7')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61f3046c",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f05706d0",
   "metadata": {},
   "source": [
    "# 4.作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9b28516",
   "metadata": {},
   "source": [
    "* 利用python函数的方法，封装 face++ 人脸检测，人脸分析和人脸对比\n",
    "* 尝试face++人脸搜索API接口 和人脸库API组\n",
    "*  宿舍人脸识别系统应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bfcf59e",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac192734",
   "metadata": {},
   "source": [
    "## （2）封装人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "fe0196c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_analyze(API_Key,API_Secret,face_tokens,attributes):\n",
    "    \"\"\"face++ 人脸分析\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_tokens\":face_tokens,\n",
    "        \"return_attributes\":attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2967823f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 179,\n",
       " 'request_id': '1647702378,4d7760f3-063b-489d-a322-e1f5a3032577',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 30},\n",
       "    'smile': {'threshold': 50.0, 'value': 93.981}},\n",
       "   'face_rectangle': {'width': 42, 'top': 81, 'left': 84, 'height': 42},\n",
       "   'face_token': '1be78a71471c6fba325f2f68e46b7d5f'}]}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze(API_Key,API_secret,'1be78a71471c6fba325f2f68e46b7d5f',\"gender,age,smiling\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "85ca586e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d18fca86",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fbcc0a2",
   "metadata": {},
   "source": [
    "## （3）封装人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "60c5b7fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_compare(API_Key,API_Secret,face_token1,face_token2):\n",
    "    \"\"\"face++ 人脸比对\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_token1\":face_token1,\n",
    "        \"face_token2\":face_token2\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c6703b92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647702429,d943939b-0d42-4528-aa38-2547b96a2ff8',\n",
       " 'time_used': 281,\n",
       " 'confidence': 36.457,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare(API_Key,API_secret,\"1be78a71471c6fba325f2f68e46b7d5f\",\"34699e256bf5be7e74a3fd2a48b150a6\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c18958aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2b4bf00",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8e85a49",
   "metadata": {},
   "source": [
    "# 5.人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4a0e3b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'cba5a14a31dc915c13728af8377367df',\n",
       " 'time_used': 133,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647916916,1a9b4afa-8e64-4820-b5d4-faffcdc51177',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_create = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":'6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft',\n",
    "    \"api_secret\":\"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\",\n",
    "    \"face_tokens\":\"34699e256bf5be7e74a3fd2a48b150a6\",\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_create,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ca3e27fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65cacf57",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ec6ea0",
   "metadata": {},
   "source": [
    "# 6. 人脸搜索API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "641e4c57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648190505,5dd50ec7-4b9e-477a-9df5-e2aef9b38ec6',\n",
       " 'time_used': 391,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': '34699e256bf5be7e74a3fd2a48b150a6'}]}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_search = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":'6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft',\n",
    "    \"api_secret\":\"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\",\n",
    "    \"face_token\":\"34699e256bf5be7e74a3fd2a48b150a6\",\n",
    "    \"faceset_token\":\"c028d431fe1099afa1a29825038055ad\"\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_search,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "be1803c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
